//
//  Dictionary+Ext.swift
//  TTCOM
//
//  Created by Toj on 7/29/22.
//

import Foundation

public extension Dictionary {
    
    mutating func cm_merge<S>(_ other: S)
    where S: Sequence, S.Iterator.Element == (key: Key, value: Value) {
        for (k ,v) in other {
            self[k] = v
        }
    }
    
    static func +(lhs: Dictionary, rhs: Dictionary) -> Dictionary {
        var result = lhs
        result.cm_merge(rhs)
        return result
    }
    
    static func +=(lhs: inout Dictionary, rhs: Dictionary) {
        lhs.cm_merge(rhs)
    }
    
    /**
     * dictionary to queries
     * e.g.: ["card": "1234", "age":"11", "name":"tao"]
     * retsult: card=1234&name=tao&age=11
     */
    var to_queryString: String {
        guard !isEmpty else { return "" }
        
        var queries = [String]()
        forEach { queries.append("\($0)=\($1)") }
        let result = queries.joined(separator: "&")
        return result
    }
}
